/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    vector<int>path;
    vector<vector<int>>res;
    vector<string>ans;
public:
    vector<string> binaryTreePaths(TreeNode* root) {
        if(root==nullptr){return ans;}
        dfs(root);
        for(int i=0;i<res.size();i++){
            ans.push_back(num_path(res[i]));
        }
        return ans;
    }
    void dfs(TreeNode* root){
        path.push_back(root->val);
        if(root->left==nullptr&&root->right==nullptr){
            res.push_back(path);
            path.pop_back();
            return;
        }
        if(root->left!=nullptr){
            dfs(root->left);
        }
        if(root->right!=nullptr){
            dfs(root->right);
        }
        path.pop_back();
    }
    string num_path(vector<int> path){
        string res="";
        res+=to_string(path[0]);
        for(int i=1;i<path.size();i++){
            res+="->";
            res+=to_string(path[i]);
        }
        return res;
    }
};